package top.huhuiyu.springboot3.task;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import top.huhuiyu.springboot3.service.api.HistoryService;

// 开启定时任务
@EnableScheduling
@Component
@RequiredArgsConstructor
@Slf4j
public class AutoDataTask {
    private final HistoryService historyService;

    // @Scheduled定时任务配置注解，initialDelay首次执行延迟，fixedDelay执行间隔
//    @Scheduled(initialDelay = 10 * 1000, fixedDelay = 60 * 1000)
//    public void test() {
//        log.debug("测试定时任务执行");
//    }

//    @Scheduled(cron = "17 21,24 10 * * ?")
//    public void test2() {
//        log.debug("测试定时任务执行2");
//    }


    @Scheduled(initialDelay = 60 * 1000, fixedDelay = 60 * 60 * 1000)
    public void history() {
        log.info("开始抓取历史上的今天的信息");
        try {
            int count = historyService.makeHistoryData();
            log.info("抓取历史上的今天信息完成，共{}条数据", count);
        } catch (Exception ex) {
            log.error("抓取历史上的今天信息出错", ex);
        }
    }

}
